package com.shixun.dds_11.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shixun.dds_11.dto.AddressPageQueryDTO;
import com.shixun.dds_11.entity.DdsAddress;
import com.shixun.dds_11.entity.DdsOrder;
import com.shixun.dds_11.mapper.DdsAddressMapper;
import com.shixun.dds_11.result.PageResult;
import com.shixun.dds_11.service.IDdsAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YSP
 * @since 2024-07-05
 */
@Service
public class DdsAddressServiceImpl extends ServiceImpl<DdsAddressMapper, DdsAddress> implements IDdsAddressService {

    @Autowired
    private DdsAddressMapper ddsAddressMapper;

    /**
     * 新增地址
     * @param ddsAddress
     * @return
     */
    public int insert(DdsAddress ddsAddress) {
        // 不允许完全不填写地址信息，地址与详细地址必须至少有一个不为空
        if (ddsAddress.getAddress() == null && ddsAddress.getDetailAddress() == null)
            return 0;
        return ddsAddressMapper.insertSelective(ddsAddress);
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    public int deleteOne(Integer id) {
        return ddsAddressMapper.deleteById(id);
    }

    /**
     * 删除用户地址
     * @param userId
     * @return
     */
    public int deleteAll(Integer userId) {
        return ddsAddressMapper.deleteByUserId(userId);
    }

    /**
     * 修改地址
     * @param ddsAddress
     * @return
     */
    public int update(DdsAddress ddsAddress) {
        // 更新的时候根据主键更新，因此主键属性不可为空
        if (ddsAddress.getId() == null) return 0;
        ddsAddress.setUserId(null);
        return ddsAddressMapper.updateByPrimaryKeySelective(ddsAddress);
    }

    /**
     * 查询
     * @param id
     * @return
     */
    public DdsAddress findOne(Integer id) {
        return ddsAddressMapper.findAllById(id);
    }

    /**
     * 查找所有
     * @param userId
     * @return
     */
    public List<DdsAddress> findAll(Integer userId) {
        return ddsAddressMapper.findAllByUserId(userId);
    }

    /**
     * 分页查询地址
     * @param addressPageQueryDTO
     * @return
     */
    public PageResult pageQuery(AddressPageQueryDTO addressPageQueryDTO) {
        // 开始分页查询
        PageHelper.startPage(addressPageQueryDTO.getPage(), addressPageQueryDTO.getPageSize());
        //调用方法
        Page<DdsAddress> page = ddsAddressMapper.pageQuery(addressPageQueryDTO);
        // 加工处理
        long total = page.getTotal();
        List<DdsAddress> records = page.getResult();
        PageResult pageResult = new PageResult(total, records);
        return pageResult;
    }
}
